<?php

namespace app\admin\controller;

use app\common\controller\BaseAdmin;
use app\admin\model\Recharge as Model;
use app\exception\ApiException;
use think\facade\Db;

class Recharge  extends BaseAdmin
{
    //需要获取的列表自定义字段内容，内容模型内定义
    public $searchField = [
        'user'=>['name'=>'username','phone','balance']
    ];

    /**
     * 初始化CURD控制器
     * @return void
     */
    public function initialize(){
        parent::initialize();
        $this->model = new Model();
    }

    /**
     * 列表Assign设置
     * 向index方法传模板页面参数
     */
    protected function indexAssign($data){
       if ($this->request->isAjax()){
           $total_amount = $this->model->withSearch($this->searchFields($data),$this->searchData($data))
               ->sum('amount');
           $day_amount = $this->model->withSearch($this->searchFields($data),$this->searchData($data))
               ->whereTime('create_time', 'today')
               ->sum('amount');
           return [
               'total_amount' =>  $total_amount,
               'day_amount' => $day_amount
           ];
       }
    }



    /**
     * 卡付通过回调
     * @return void
     */
    public function card(){
        $order_sn = $this->request->param('order_sn');
        $callback = $this->request->domain().'/callback/card/recharge';
        $res = curlPost($callback,[
            'order_sn' => $order_sn,
        ]);
        if ($res=='SUCCESS'){
             $this->success('操作成功');
        }else{
             $this->error('操作失败');
        }
    }
}